<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Register_model extends DQ_Model 
{
    public function __construct()
    {
        parent::__construct();
    }

    public function create()
    {
        $captcha_error = $this->_checkCaptcha();

        if($captcha_error)
        {
            return $captcha_error;
        }

        $checkedData = $this->checkInput(__FUNCTION__,0);

        //密码一致
        if($checkedData['password'] != $checkedData['repassword'])
        {
            return array('error'=>6003,'msg'=>$this->error_code[6003],'data'=>0);
        }

        $exists = $this->_checkAccountExist($checkedData['email'],$checkedData['username']);

        if(!$exists)
        {
            $uid = $this->_create($checkedData['email'],$checkedData['username'],$checkedData['password']);

            if($uid)
            {
                $this->session->set_userdata("uid", $uid);
                $this->session->set_userdata("username", $checkedData['username']);

                return array('error'=>0,'msg'=>$this->error_code[0],'data'=>$uid);
            }
            else
            {
                return array('error'=>6002,'msg'=>$this->error_code[6002],'data'=>0);
            }
        }
        elseif ($exists == 1)
        {
            return array('error'=>6001,'msg'=>$this->error_code[6001],'data'=>0);
        }
        else
        {
            return array('error'=>6004,'msg'=>$this->error_code[6004],'data'=>0);
        }
    }

    private function _create($email,$username,$password)
    {
        $this->load->helper('string');
        $salt         = random_string ('alpha', 8);   // 生成8位掩码
        $mix_password = strtoupper(md5(strtoupper(md5 ($password . $salt))));      // 生成混淆的md5值

        $this->db->trans_begin();

        $this->db->insert('uc_member',array(
            'email' => $email,
            'username' => $username,
            'password' => $mix_password,
            'salt' => $salt
        ));
        $uid = $this->db->insert_id();

        $this->db->insert('uc_member_detail',array(
            'uid' => $uid,
            'birthday' => '0000-00-00',
            'height' => 0,
            'area' => '',
            'player_type' => 0
        ));

        if($this->db->trans_status() === FALSE)
        {
            $this->db->trans_rollback();
            return false;
        }
        else
        {
            $this->db->trans_commit();
            return $uid;
        }
    }

    //帐号是否已存在
    private function _checkAccountExist($email,$username)
    {
        $this->db->where('email',$email);
        $this->db->or_where('username',$username);
        $query = $this->db->get('uc_member');

        foreach($query->result_array() as $value)
        {
            if($value['email'] == $email)
            {
                return 1;
            }
            elseif($value['username'] == $username)
            {
                return 2;
            }
        }

        return 0;
    }

}
